﻿/*
 *	(*^。^*) TFrameWork License (*^。^*)
 */

/*
 *  Title :
 *
 *  Description :
 *
 *
 *  Date :
 *
 *  Author : DengPing
 *
 *  Modify :
 */
using System;
using UnityEditor;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{

    [CustomEditor(typeof(ActionEditor))]
    public partial class ActionEditorInspector : Editor
    {
        public static ActionEditorInspector Instance;
        public static TrackItemStyleBase currentTrackItemStyle { get; private set; }

        public static void SetTrackItem(TrackItemStyleBase trackItemStyle)
        {
            if (currentTrackItemStyle != null)
            {
                currentTrackItemStyle.OnUnSelect();
            }

            currentTrackItemStyle = trackItemStyle;
            currentTrackItemStyle.OnSelect();

            // 避免已经打开了Inspector，不刷新数据
            if (Instance != null) Instance.Show();
        }

        private void OnDestroy()
        {
            // 说明窗口卸载
            if (currentTrackItemStyle != null)
            {
                currentTrackItemStyle.OnUnSelect();
                currentTrackItemStyle = null;
            }
        }

        private VisualElement root;

        public override VisualElement CreateInspectorGUI()
        {
            Instance = this;
            root = new VisualElement();
            Show();
            return root;
        }

        public static void Refresh()
        {
            if (Instance != null)
            {
                Instance.Show();
            }
        }

        public void Show()
        {
            Clean();
            if (currentTrackItemStyle == null) return;

            // TODO:补充其他类型
            Type itemType = currentTrackItemStyle.GetType();
            if (itemType == typeof(SignalEventItemStyle))
            {
                var signal = new SignalInspector();
                signal.Show(root);
            }
            else if (itemType == typeof(AnimationEventItemStyle))
            {
                var animation = new AnimationInspector();
                animation.Show(root);
            }
            else if (itemType == typeof(AudioEventItemStyle))
            {
                var audio = new AudioInspector();
                audio.Show(root);
            }
            else if (itemType == typeof(GameObjectEventItemStyle))
            {
                var partcle = new PrefabInspector();
                partcle.Show(root);
            }
            else if (itemType == typeof(TriggerEventItemStyle))
            {
                var trigger = new TriggetInspector();
                trigger.Show(root);
            }
        }

        private void Clean()
        {
            if (root != null)
            {
                for (int i = root.childCount - 1; i >= 0; i--)
                {
                    root.RemoveAt(i);
                }
            }

        }
    }
}
